package com.xjw.hospital.controller;

import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import com.xjw.annotation.FXController;
import com.xjw.annotation.FXRedirect;
import com.xjw.annotation.FXWindow;
import com.xjw.coreutil.cache.CacheUtil;
import com.xjw.coreutil.common.StringUtil;
import com.xjw.entity.FXBaseController;
import com.xjw.hospital.entity.DocInfo;
import com.xjw.hospital.utils.ToolUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.text.Text;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author 徐军文
 * @since 2022/8/26 20:10
 */
@FXController(path = "fxml/appointTask.fxml")
@FXWindow(title = "预约抢号", icon = "image/icon.png")
public class AppointTaskController extends FXBaseController implements Initializable {

    @FXML
    private Text docNameEle;

    @FXML
    private Text deptNameEle;

    @FXML
    private ComboBox<String> startHEle;

    @FXML
    private ComboBox<String> startMEle;

    @FXML
    private ComboBox<String> endHEle;

    @FXML
    private ComboBox<String> endMEle;

    private final ObservableList<String> hours = FXCollections.observableArrayList();

    private final ObservableList<String> mins = FXCollections.observableArrayList();

    private DocInfo docInfo;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initHours();
        initMins();
        startHEle.setItems(hours);
        endHEle.setItems(hours);
        startMEle.setItems(mins);
        endMEle.setItems(mins);
        startHEle.getSelectionModel().select(17);
        startMEle.getSelectionModel().select(58);
        endHEle.getSelectionModel().select(18);
        endMEle.getSelectionModel().select(3);
    }

    private void initHours() {
        for (int i = 0;i <= 9;i++) {
            hours.add("0" + i);
        }
        for (int i = 10;i <= 23;i++) {
            hours.add(String.valueOf(i));
        }
    }

    private void initMins() {
        for (int i = 0;i <= 9;i++) {
            mins.add("0" + i);
        }
        for (int i = 10;i <= 59;i++) {
            mins.add(String.valueOf(i));
        }
    }

    @Override
    public void onShow() {
        docInfo = CacheUtil.get("appoint");
        deptNameEle.setText(docInfo.getDeptName());
        docNameEle.setText(docInfo.getDocName());
    }

    @FXML
    @FXRedirect()
    public String appointTask() {
        String startH = startHEle.getSelectionModel().getSelectedItem();
        String startM = startMEle.getSelectionModel().getSelectedItem();
        String endH = endHEle.getSelectionModel().getSelectedItem();
        String endM = endMEle.getSelectionModel().getSelectedItem();
        String startCron = "0 " + startM + " "  + startH
                + " * * ? ";
        String endCron = "0 " + endM + " "  + endH
                + " * * ? ";
        String appointFlag = CacheUtil.get("appoint" + docInfo.getDocId());
        List<String> msgCache = CacheUtil.get("msgCache");
        if (StringUtil.isNotEmpty(appointFlag)) {
            msgCache.add("任务已预约");
            return "MainController";
        }
        CacheUtil.put("appoint" + docInfo.getDocId(), "true");
        // 设置抢票定时任务
        String scheduleStartId = CronUtil.schedule(startCron, (Task) () -> {
            if (docInfo != null) {
                ToolUtil.getTicket(docInfo, 100);
            }
        });

        // 结束抢票任务
        String scheduleEndId = CronUtil.schedule(endCron, (Task) () -> {
            CacheUtil.remove("appoint" + docInfo.getDocId());
            ToolUtil.stopGetTicket(docInfo);
        });
        CacheUtil.put("scheduleStartId" + docInfo.getDocId(), scheduleStartId);
        CacheUtil.put("scheduleEndId" + docInfo.getDocId(), scheduleEndId);
        msgCache.add(startH + ":"
                + startM + " 开始抢票");
        msgCache.add(endH + ":"
                + endM + " 结束抢票");
        return "MainController";
    }

    @FXML
    @FXRedirect()
    public String cancelTask() {
        return "MainController";
    }

}
